const pending = 'pending'
const resolved = 'resolved'
const rejected = 'rejected'
const resolvepromise = (promise2, x, resolve, reject) => {
  if (promise2 === x) {
    return reject(new TypeError('不能返回自身'))
  }
  if (typeof x === 'object' && typeof x !== null || typeof x === 'function') {
    //由于测试的时候即调用成功，也调用失败resolve(),reject()
    try {
      let then = x.then
      if (typeof then === 'function') {//是一个promise
        then.call(x, y => {
          resolvepromise(promise2, y, resolve, reject)//什么意思就是说这个，resolve可能继续返回一个promise，所以想递归不断得到一个普通值后再去返回

        }, r => {
          reject(r)
        })

      } else {
        resolve(x)//这里就是说这个x亦可能是普通值then里面返回一个普通值自然，调用promise2的一个resolve(x)
      }

    } catch (error) {
      reject(error)
    }

  }

}
class Promise {
    constructor(executor) {
        this.status = pending
        this.value = undefined
        this.reason = undefined
        this.onfullfiledCallbacks = []
        this.onrejectedCallbacks = []
        let resolve = (value) => {
            if (this.status === pending) {//加if判断是为了能够限制，状态不能随意改变
                this.value = value
                this.status = resolved
                this.onfullfiledCallbacks.forEach(fn => fn())
            }
        }
        let reject = (reason) => {
            if (this.status === pending) {
                this.status = rejected
                this.reason = reason
                this.onrejectedCallbacks.forEach(fn => fn())
            }//用箭头函数是因为要来去继承外部代码快的this

        }
        try {
            executor(resolve, reject)
        } catch (error) {
            reject(error)
        }
    }

    then(onfullfiled, onrejected) {
        let promsie2 = new Promise((resolve, reject) => {
            //同步的情况
            if (this.status === resolved) {
                setTimeout(() => {
                    try {
                        let x = onfullfiled(this.value)//这个是then()方法返return 的一个结果
                        ResolvePromise(x, promsie2, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                })

                if (this.status === rejected) {
                    setTimeout(() => {
                        try {
                            let x = onrejected(this.reason)//这个是then()方法返return 的一个结果
                            ResolvePromise(x, promsie2, resolve, reject)
                        } catch (error) {
                            reject(error)
                        }
                    })
                }
                //异步的情况
                if (this.status === pending) {
                    this.onfullfiledCallbacks.push(() => {
                        setTimeout(() => {
                            try {
                                let x = onfullfiled(this.value)//这个是then()方法返return 的一个结果
                                ResolvePromise(x, promsie2, resolve, reject)
                            } catch (error) {
                                reject(error)
                            }
                        })

                    })
                    this.onrejectedCallbacks.push(() => {
                        setTimeout(() => {
                            try {
                                let x = onrejected(this.reason)//这个是then()方法返return 的一个结果
                                ResolvePromise(x, promsie2, resolve, reject)
                            } catch (error) {
                                reject(error)
                            }
                        })

                    })

                }

            }



        })
        return promsie2

    }
}

module.exports = Promise